//
// Option.cpp
//
// $Id: //poco/1.4/Util/src/Option.cpp#1 $
//
// Library: Util
// Package: Options
// Module:  Option
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Util/Option.h"
#include "Poco/Util/OptionException.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/String.h"
#include <algorithm>


using Poco::icompare;


namespace Poco {
namespace Util {


Option::Option(): 
	m_required(false), 
	m_repeatable(false), 
	m_argRequired(false),
	m_pCallback(0),
	m_pConfig(0)
{
}


Option::Option(const Option& option):
	m_shortName(option.m_shortName),
	m_fullName(option.m_fullName),
	m_description(option.m_description),
	m_required(option.m_required),
	m_repeatable(option.m_repeatable),
	m_argName(option.m_argName),
	m_argRequired(option.m_argRequired),
	m_group(option.m_group),
	m_binding(option.m_binding),
	m_pCallback(option.m_pCallback),
	m_pConfig(option.m_pConfig)
{
	if (m_pCallback) m_pCallback = m_pCallback->clone();
	if (m_pConfig) m_pConfig->duplicate();
}


Option::Option(const std::string& fullName, const std::string& shortName):
	m_shortName(shortName),
	m_fullName(fullName),
	m_required(false),
	m_repeatable(false),
	m_argRequired(false),
	m_pCallback(0),
	m_pConfig(0)
{
}


Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required):
	m_shortName(shortName),
	m_fullName(fullName),
	m_description(description),
	m_required(required),
	m_repeatable(false),
	m_argRequired(false),
	m_pCallback(0),
	m_pConfig(0)
{
}


Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argRequired):
	m_shortName(shortName),
	m_fullName(fullName),
	m_description(description),
	m_required(required),
	m_repeatable(false),
	m_argName(argName),
	m_argRequired(argRequired),
	m_pCallback(0),
	m_pConfig(0)
{
}


Option::~Option()
{
	if (m_pConfig) m_pConfig->release();
	delete m_pCallback;
}


Option& Option::operator = (const Option& option)
{
	if (&option != this)
	{
		Option tmp(option);
		swap(tmp);
	}
	return *this;
}


void Option::swap(Option& option)
{
	std::swap(m_shortName, option.m_shortName);
	std::swap(m_fullName, option.m_fullName);
	std::swap(m_description, option.m_description);
	std::swap(m_required, option.m_required);
	std::swap(m_repeatable, option.m_repeatable);
	std::swap(m_argName, option.m_argName);
	std::swap(m_argRequired, option.m_argRequired);
	std::swap(m_group, option.m_group);
	std::swap(m_binding, option.m_binding);
	std::swap(m_pCallback, option.m_pCallback);
	std::swap(m_pConfig, option.m_pConfig);
}

	
Option& Option::shortName(const std::string& name)
{
	m_shortName = name;
	return *this;
}


Option& Option::fullName(const std::string& name)
{
	m_fullName = name;
	return *this;
}

	
Option& Option::description(const std::string& text)
{
	m_description = text;
	return *this;
}

	
Option& Option::required(bool flag)
{
	m_required = flag;
	return *this;
}


Option& Option::repeatable(bool flag)
{
	m_repeatable = flag;
	return *this;
}

	
Option& Option::argument(const std::string& name, bool required)
{
	m_argName     = name;
	m_argRequired = required;
	return *this;
}

	
Option& Option::noArgument()
{
	m_argName.clear();
	m_argRequired = false;
	return *this;
}


Option& Option::group(const std::string& group)
{
	m_group = group;
	return *this;
}


Option& Option::binding(const std::string& propertyName)
{
	return binding(propertyName, 0);
}


Option& Option::binding(const std::string& propertyName, AbstractConfiguration* pConfig)
{
	m_binding = propertyName;
	if (m_pConfig) m_pConfig->release();
	m_pConfig = pConfig;
	if (m_pConfig) m_pConfig->duplicate();
	return *this;
}


Option& Option::callback(const AbstractOptionCallback& cb)
{
	m_pCallback = cb.clone();
	return *this;
}


bool Option::matchesShort(const std::string& option) const
{
	return option.length() > 0 
		&& !m_shortName.empty() && option.compare(0, m_shortName.length(), m_shortName) == 0;
}


bool Option::matchesFull(const std::string& option) const
{
	std::string::size_type pos = option.find_first_of(":=");
	std::string::size_type len = pos == std::string::npos ? option.length() : pos;
	return len == m_fullName.length()
		&& icompare(option, 0, len, m_fullName, 0, len) == 0;
}


bool Option::matchesPartial(const std::string& option) const
{
	std::string::size_type pos = option.find_first_of(":=");
	std::string::size_type len = pos == std::string::npos ? option.length() : pos;
	return option.length() > 0 
		&& icompare(option, 0, len, m_fullName, 0, len) == 0;
}


void Option::process(const std::string& option, std::string& arg) const
{
	std::string::size_type pos = option.find_first_of(":=");
	std::string::size_type len = pos == std::string::npos ? option.length() : pos;
	if (icompare(option, 0, len, m_fullName, 0, len) == 0)
	{
		if (takesArgument())
		{
			if (argumentRequired() && pos == std::string::npos)
				throw MissingArgumentException(m_fullName + " requires " + argumentName());
			if (pos != std::string::npos)
				arg.assign(option, pos + 1, option.length() - pos - 1);
			else
				arg.clear();
		}
		else if (pos != std::string::npos)
		{
			throw UnexpectedArgumentException(option);
		}
		else arg.clear();
	}
	else if (!m_shortName.empty() && option.compare(0, m_shortName.length(), m_shortName) == 0)
	{
		if (takesArgument())
		{
			if (argumentRequired() && option.length() == m_shortName.length())
				throw MissingArgumentException(m_shortName + " requires " + argumentName());
			arg.assign(option, m_shortName.length(), option.length() - m_shortName.length());
		}
		else if (option.length() != m_shortName.length())
		{
			throw UnexpectedArgumentException(option);
		}
		else arg.clear();
	}
	else throw UnknownOptionException(option);
}


} } // namespace Poco::Util
